package com.cqc.wavesidebardemo01;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.gjiazhe.wavesidebar.WaveSideBar;

import java.util.ArrayList;
import java.util.List;

public class CustomActivity extends AppCompatActivity {

    private Toast toast;
    private RecyclerView recyclerView;
    private WaveSideBar waveSideBar;
    private Context context;
    private List<Contact> contacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_right);
        context = this;
        toast = Toast.makeText(CustomActivity.this, "", Toast.LENGTH_SHORT);
        findViews();

        initRecyclerView();
        initWaveSideBar();
    }

    private void initRecyclerView() {
        initData();
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        MyRecyclerAdapter adapter = new MyRecyclerAdapter(contacts);
        recyclerView.setAdapter(adapter);
    }


    private void initWaveSideBar() {
        waveSideBar.setIndexItems("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
        waveSideBar.setOnSelectIndexItemListener(new WaveSideBar.OnSelectIndexItemListener() {
            @Override
            public void onSelectIndexItem(String index) {
                toast.setText(index);
                toast.show();
                for (int i = 0; i < contacts.size(); i++) {
                    if (index.equals(contacts.get(i).getIndex())) {
                        recyclerView.scrollToPosition(i);
                        return;
                    }
                }
            }
        });
    }

    private void findViews() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        waveSideBar = (WaveSideBar) findViewById(R.id.waveSideBar);
    }

    private void initData() {
        contacts = new ArrayList<>();
        contacts.add(new Contact("0", "白虎"));
        contacts.add(new Contact("0", "常羲"));
        contacts.add(new Contact("1", "嫦娥"));
        contacts.add(new Contact("1", "二郎神"));
        contacts.add(new Contact("2", "伏羲"));
        contacts.add(new Contact("2", "观世音"));
        contacts.add(new Contact("3", "精卫"));
        contacts.add(new Contact("3", "夸父"));
        contacts.add(new Contact("4", "女娲"));
        contacts.add(new Contact("4", "哪吒"));
        contacts.add(new Contact("5", "盘古"));
        contacts.add(new Contact("5", "青龙"));
        contacts.add(new Contact("6", "如来"));
        contacts.add(new Contact("6", "孙悟空"));
        contacts.add(new Contact("7", "沙僧"));
        contacts.add(new Contact("7", "顺风耳"));
        contacts.add(new Contact("8", "太白金星"));
        contacts.add(new Contact("8", "太上老君"));
        contacts.add(new Contact("8", "羲和"));
        contacts.add(new Contact("9", "玄武"));
        contacts.add(new Contact("9", "猪八戒"));
        contacts.add(new Contact("9", "朱雀"));
        contacts.add(new Contact("9", "祝融"));
    }

}
